<?php
namespace Common\Model;
use Common\Model\AiModel;
class AdModel extends AiModel {
    protected $_auto = array (
        array('start_time','strtotime',3,'function'),
        array('end_time','strtotime',3,'function')
    );
    
    public function getAllAds(){
        
        if(IDENTITY=='doctor'){
            $where2 = " AND push_type = 2 ";
        }else{
            $where2 = " AND push_type = 1 ";
            if(D('Button')->getExtParams()){
                $where2 = " AND push_type = 3 ";
            }
        }
        
        if(intval(UID)==0){
            $time = time();
            $where = " start_time <= $time AND end_time >= $time AND state = 1 $where2 ";
            $res = $this->where($where)->order('RAND() DESC')->limit(5)->select();
        }else{
            //判断是否怀孕了
            $is_birth = is_birth(intval(UID));
            if($is_birth == '0'){
                $weeks = get_pregnant_days(intval(UID));
                $weeks = floor($weeks/7);
                $num   = $weeks%7;
                if($weeks<=5 || $weeks>42) $weeks = 5;
            }else{
                $weeks = get_children_month(intval(UID));
            }
            
            $where = " state = 1 $where2 ";
            $res = $this->where($where)->order('RAND() DESC')->limit(5)->select();
            $time = strtotime(date("Y-m-d",time()));
            $start_time = $time-$num*3600;
            $end_time   = $time+(7-$num)*3600;
            foreach($res as $key=>&$val){
                $val['start_time'] = (string)$start_time;
                $val['end_time']   = (string)$end_time;
            }
        }
        return $res;
    }
}